import Vue from 'vue'
import VueRouter from 'vue-router'
import RoomListView from "@/views/house/RoomListView.vue";
import StaffListView from "@/views/admin/StaffListView.vue";
import UserLoginView from "@/views/user/UserLoginView.vue";
import UserManageView from "@/views/user/UserManageView.vue";
import PropertyFeeView from "@/views/fee/PropertyFeeView.vue";
import UsersView from "@/views/user/UsersView.vue";
import PropertyFee from"@/views/PropertyFee.vue";
//未登录自动跳转到登录界面

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manage',
    component: () =>import('../views/Manage'),
    redirect: '/admin/home',
    children: [
      { path:'admin/home', name: '首页', component: () => import('../views/Home.vue')},
      { path: 'admin/room', name: '房屋管理', component:RoomListView},
      { path:'admin/staff', name: '员工管理', component:StaffListView},
      // {path:'admin/staff/insert',name:'添加员工',component:StaffInsertView},
      // {path: 'admin/staff/update',name: '修改员工',component:StaffUpdateView},
      {path: 'admin/car' ,name: '车辆管理',component: () => import('../views/Car.vue')},
      {path: 'admin/accessrecord',name: '出入记录',component: () => import('../views/AccessRecord.vue')},
      {path: 'admin/Visitor',name: '访客记录',component: () => import('../views/Visitor.vue')},
      {path: 'admin/propertyFee',name: '物业费',component:PropertyFeeView},
      {path:'user/users',name: '业主信息',component:UsersView},

      {path: '/admin/Notice',name: '小区公告',component: () => import('../views/Notice.vue')},
      {path:'/admin/repairs',name:'设备报修',component:() => import('../views/equipment/Repairs.vue')},
      {path:'/admin/equipmentInfo',name:'设备信息',component:() => import('../views/equipment/EquipmentInfo.vue')},
      {path:'/admin/handlerRepair',name:'处理报修',component:() => import('../views/equipment/HandlerRepair.vue') },
      {path:'/admin/PropertyFee',name:'物业费',component:() => import('../views/PropertyFee.vue')},
    ],
  },
  {
    path: '/user/login',
    name: '业主/租客登录',
    component:UserLoginView
  },
  {
    path: '/user/manage',
    name: '业主首页',
    component:UserManageView,
  },
  {
    path: '/user/users',
    name: '业主信息',
    component:UsersView,
    //meta:{requiresAuth:true},//需要登录
  },
  {
    path: '/admin/HandleUpdate',
    name: 'HandleUpdate',
    component: () => import('../views/HandleUpdate.vue')
  },
  {
    path: 'PropertyFee',
    name: '物业费',
    component: () => import('../views/PropertyFee')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAdminPath = to.path.startsWith('/admin/');
  const isUserPath = to.path.startsWith('/user/');
  const isLoginPage = to.path === '/login' || to.path === '/user/login';

  // 如果是登录页，直接放行（无需 token）
  if (isLoginPage) {
    next();
    return;
  }

  // 如果是管理或用户路径，检查 token
  if (isAdminPath || isUserPath) {
    const token = window.localStorage.getItem('token');
    const userToken = window.localStorage.getItem('userToken');
    if (isAdminPath){
      if(token)
        next();
      else
        next('/admin/login');
    }
    else if (isUserPath){
      if(userToken)
        next();
      else
        next('/user/login');
    }
  } else {
    next(); // 其他路径直接放行
  }
});
export default router